package com.oxy.action;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.apache.log4j.Logger;

import com.oxy.action.util.ActionUtil;
import com.oxy.svc.StaticSvc;
import com.oxy.util.Oxy;

public class MiscAction extends BaseAction {
	private static final long serialVersionUID = -8922445744211736538L;

	private final static Logger logger = Oxy.getLogManager().getLoggerCommon();
	
	private static String imgDir = Oxy.getCfgManager().App().FSPATH_STATIC_IMG;
	
	public void getCss(){
		String src=ActionUtil.getRParam("src");
		StaticSvc svc = (StaticSvc)this.getSvcHolder().getSvcByIfClass(StaticSvc.class);
		ActionUtil.responseWrite(svc.getCssContent(src));
	}
	
	public void getJs(){
		String src=ActionUtil.getRParam("src");
		StaticSvc svc = (StaticSvc)this.getSvcHolder().getSvcByIfClass(StaticSvc.class);
		ActionUtil.responseWrite(svc.getJsContent(src));
	}
	
	public void getImg(){
		String src=ActionUtil.getRParam("src");
		if(src!=null && !src.equals("")){
			File f=new File(imgDir+"/"+src);
			FileInputStream fis;
			try {
				fis = new FileInputStream(f);
				BufferedInputStream bis = new BufferedInputStream(fis);
				byte[] buf=new byte[1024];
				int len=0;
				while((len=bis.read(buf))!=-1){
					ActionUtil.getP().getOutputStream().write(buf, 0, len);
				}
				ActionUtil.getP().getOutputStream().flush();
			} catch (FileNotFoundException e) {
				logger.error(e.getMessage());
			} catch (IOException e) {
				logger.error(e.getMessage());
			}
		}
	}
}
